<?php
/**
 * 访问统计
 * Created by PhpStorm.
 * User: xcx
 * Date: 2017/07/03
 * Time: 20:19
 */
class Controller_Adminmall_Visit extends Controller_Adminmall_Abstract
{

    private  $DB_SERVER = 'xhs_slave';

    /*
     * 列表
     */
    public function listAction(){
//        $this->openDebug();
        global $_F;
        $page = FRequest::getInt("page");
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-6 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }


        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);


        if($end_time < $start_time){
            $this->error("选择的时间不对", $_F['refer']);
        }
        $data = array();
        $start_time_tm = $start_time;
        while(true){
            $result = array();
            $where['date'] = $start_time_tm;
            $userTable = new FTable("visit_log", '', $this->DB_SERVER);
            $list = $userTable->fields("count(*) as c, type ")
                ->where($where)
                ->group(array('type'))
                ->select();

            $orderTable = new FTable('orders', '', $this->DB_SERVER);
            $orders_count = $orderTable->where(array('pay_time' => array('gte' => $start_time_tm.' 00:00:00', 'lte' => $start_time_tm. ' 23:59:59' )))->count();
            if($list){
                foreach($list as $k => $v){
                    $result[$v['type']] = intval($v['c']);
                }
            }else{
                $result[1] = 0;
                $result[2] = 0;
                $result[3] = 0;
                $result[4] = 0;
                $result[5] = 0;
                $result[6] = 0;
                $result[7] = 0;
            }

            $result['orders_count'] = $orders_count;
            $data[$start_time_tm] = $result;

            $tm = strtotime($start_time_tm) + 86400;
            $start_time_tm = date("Y-m-d", $tm);
            if($start_time_tm>$end_time){
                break;
            }
        }

        $data = array_reverse($data);

        $total = array();
        foreach($data as $k => $v){
            $total['1']  = $v['1'] + $total['1'];
            $total['2']  = $v['2'] + $total['2'];
            $total['3']  = $v['3'] + $total['3'];
            $total['4']  = $v['4'] + $total['4'];
            $total['5']  = $v['5'] + $total['5'];
            $total['6']  = $v['6'] + $total['6'];
            $total['7']  = $v['7'] + $total['7'];
            $total['orders_count']  = $v['orders_count'] + $total['orders_count'];
        }
        $this->assign('total', $total);
//        var_dump($total);

//        var_dump($data);
//        $pagerInfo = $userTable->getPagerInfo();
//        $this->assign('page_info', $pagerInfo);
//        var_dump($result);
        $this->assign("data", $data);
        $this->display('visit/list');




    }

    public function logAction(){
        $date = FRequest::getString('date');
        $type = FRequest::getInt('type');
        $page = FRequest::getInt('page');
        $where['t1.date'] = $date;
        $where['t1.type'] = $type;
//        $where['ua.is_default'] = 1;
        $userTable = new FTable("visit_log", 't1', $this->DB_SERVER);
        $dataList = $userTable->fields('t1.*,t2.username')
            ->where($where)
            ->leftJoin('user','t2','t2.uid=t1.uid')
//            ->leftJoin('user_address', 'ua', 'ua.uid=t1.uid')
            ->page($page)
            ->limit(20)
            ->select();

//        , ua.city, ua.county, ua.address as addr, ua.housemember

        foreach($dataList as &$item){
            $ret = Service_Address::getDefaultAddress($item['uid']);
            if($ret){
                $item['add'] = "{$ret['city']}|{$ret['county']}|{$ret['address']} |{$ret['housemember']}";
            }else{
                $item['add'] = "";
            }
        }


        $pagerInfo = $userTable->getPagerInfo();
        $this->assign('page_info',$pagerInfo);

        $result = FDB::fetchFirst("SELECT count(*) AS `count` FROM user_address WHERE uid 
                              IN(SELECT uid FROM visit_log WHERE `type`={$type} AND `date`='{$date}')",$this->DB_SERVER);

//        $url = 'http://www.xiaoyoo.cn:20003/user/input'.'?'.http_build_query($data);


        $IpUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php";
        $para = array('format' => 'json');
        foreach($dataList as &$item){
            $para['ip'] = $item['ip'];
            $url = $IpUrl.'?'.http_build_query($para);
            $ret = json_decode(FHttp::get($url),true);
            $item['address'] = "{$ret['province']} {$ret['city']}";
        }
        $this->assign('dataList',$this->getTypeText($dataList));

        $this->assign('result',$result);
        $this->display('visit/log');
    }

    public function teAction(){
        $IpUrl = "http://int.dpool.sina.com.cn/iplookup/iplookup.php";
        $para = array('format' => 'json');
        $para['ip'] = '175.11.206.158';
        $url = $IpUrl.'?'.http_build_query($para);
        $result = json_decode(FHttp::get($url),true);

        FResponse::output($result);
        exit();
    }

    private function getTypeText($dataList){
        $addressTable = new FTable("user_address", '', $this->DB_SERVER);
        foreach ($dataList as &$data){
            switch ($data['type']){
                case 1:
                    $data['type_text'] = '进入首页';
                    break;
                case 2:
                    $data['type_text'] = '确认订单页';
                    break;
                case 3:
                    $data['type_text'] = '不在配送范围';
                    break;
                case 4:
                    $data['type_text'] = '生成订单';
                    break;
                case 5:
                    $data['type_text'] = '订单页跳地址列表页';
                    break;
                case 6:
                    $data['type_text'] = '添加地址';
                    break;
                case 7:
                    $data['type_text'] = '跳转百度地图';
                    break;
                default:
                    $data['type_text'] = '';
            }
            $data['addressNum'] = $addressTable->where(array("uid"=>$data['uid']))->count();
        }
        return $dataList;
    }

    /**
     * 老用户访问统计
     */
    public function oldUserTotalAction(){
        global $_F;
//        $this->openDebug();
        $start_time = FRequest::getString("begin_date");
        $end_time = FRequest::getString('end_date');

        $where = array();

        if ($start_time < 1) {
            $start_time = date('Y-m-d', strtotime("-2 days"));
        }
        if ($end_time < 1) {
            $end_time = date('Y-m-d', time());
        }


        $this->assign("start_time", $start_time);
        $this->assign("end_time", $end_time);


        if($end_time < $start_time){
            $this->error("选择的时间不对", $_F['refer']);
        }

        $whereU = array();
        $whereO = array();
        $data = array();
        $start_time_tm = $start_time;
        while(true){
            $where['vl.date']['eq'] = $start_time_tm;
            $where['vl.date']['gt'] = "u.create_time";
            $logTable = new FTable("visit_log", 'vl', $this->DB_SERVER);
            //老用户首页人数统计
            $where['vl.type']=1;
//            $count_home = $logTable->leftJoin('user', 'u', 'u.uid=vl.uid')->where($where)->count();
            $sql = "SELECT COUNT(*) as c FROM `visit_log` as vl
left join `user` as u on u.uid=vl.uid WHERE vl.date = '{$start_time_tm}' AND vl.date > u.create_time AND vl.type = 1";
            $count_home = FDB::fetch($sql, $this->DB_SERVER);

            //老用户生成订单人数
            $where['vl.type']=4;
//            $count_order = $logTable->where($where)->count();
            $sql = "SELECT COUNT(*) as c FROM `visit_log` as vl
left join `user` as u on u.uid=vl.uid WHERE vl.date = '{$start_time_tm}' AND vl.date > u.create_time AND vl.type = 4";
            $count_order = FDB::fetch($sql, $this->DB_SERVER);

            //老用户人数统计
            $userTable = new FTable('user', 'u', $this->DB_SERVER);
            $whereU['u.create_time'] = array('lt' => $start_time_tm);
            $sql = "SELECT COUNT(*) as c FROM `user`  WHERE create_time < '{$start_time_tm}' AND uid in(SELECT uid FROM orders WHERE `status`=5 GROUP BY uid)";
            $count_old_user = FDB::fetch($sql, $this->DB_SERVER);

            //老用户支付人数
            $orderTable = new FTable('orders', 'o', $this->DB_SERVER);
            $whereO['u.create_time'] = array('lt' => $start_time_tm);
            $whereO['o.status'] = 5;
            $whereO['o.create_time'] = array(
                'gte' => "{$start_time_tm} 00:00:00",
                'lte' => "{$start_time_tm} 23:59:59"
            );
            $count_pay = $orderTable->leftJoin('user', 'u', 'u.uid=o.uid')->where($whereO)->count();

            $data[$start_time_tm] = array(
                'count_home' => $count_home[0]['c'],
                'count_order' => $count_order[0]['c'],
                'count_old_user' => $count_old_user[0]['c'],
                'count_pay' => $count_pay,
            );

            $tm = strtotime($start_time_tm) + 86400;
            $start_time_tm = date("Y-m-d", $tm);
            if($start_time_tm>$end_time){
                break;
            }
        }

//        var_dump($data);
//        FResponse::output($data);
//        exit();
        $data = array_reverse($data);
        $this->assign('data', $data);
        $this->display('visit/old-user-tj');
    }

}